﻿@page "/sysseting/index"
@using TMV.Application.SocketConfig.Service;
@using TMV.DTO.SocketConfig;
@using TMV.DTO;
@using TMV.Web.Pages.SysSetting.ViewModel;
@inherits ProCompontentBase
@inject NavigationManager Nav
@inject ISocketConfigService socketConfigService
@attribute [Authorize]
<MCard Class="mt-6 full-card">
    <MCardText Class="pa-6">
        <MRow>
            <MCol Md=11 Sm=12 Class="d-flex block-center">
                
            </MCol>
            <MCol Md=1 Sm=12 Class="d-flex block-center">
                <MButton Color="primary" MinWidth=80 Height=32 OnClick="()=>_visible=true">
                    新增
                </MButton>
            </MCol>
        </MRow>
    </MCardText>
    <MDataTable Headers="_headers" Items=" userPage.Datas" TItem="SocketConfigDto" ItemsPerPage="userPage.PageSize"
                ServerItemsLength="userPage.PageCount"
                Loading="_loading">
        <HeaderColContent Context="header">
            <span class="text-subtitle">@header.Text</span>
        </HeaderColContent>
        <ItemColContent>
            @switch (context.Header.Value)
            {

                case nameof(SocketConfigDto.Status):
                        @if (context.Item.Status)
                        {
                        <MChip Color="@($"info-lighten-5")" TextColor="info" Ripple="false">
                            <span>已连接</span>
                            </MChip>
                        }
                        else
                        {
                        <MChip Color="@($"error-lighten-5")" TextColor="error" Ripple="false">
                            <span>未连接</span>
                            </MChip>
                        }
                    break;
                case "actions":
                    <MIcon Small Class="mr-2" OnClick="()=>EditItem(context.Item)">mdi-pencil</MIcon>
                    <MIcon Small OnClick="async()=>await DeleteItem(context.Item)">mdi-delete</MIcon>
                    break;
                default:
                    @context.Value
                    break;
            }

        </ItemColContent>
    </MDataTable>

    <MDialog @bind-Value="_visible"
             MaxWidth="800">
        <MCard>
            <MCardTitle>
                <span class="text-h5">@FormTitle</span>
            </MCardTitle>

            <MCardText>
                <MContainer>
                    <MRow>
                        <MCol Cols="12"
                              Sm="6"
                              Md="4">
                            <MTextField @bind-Value="_editedItem.Code" Clearable
                                        Label="业务编码"></MTextField>
                        </MCol>
                        <MCol Cols="12"
                              Sm="6"
                              Md="4">
                            <MTextField @bind-Value="_editedItem.Ip"   Label="IP地址"></MTextField>
                        </MCol>
                        <MCol Cols="12"
                              Sm="6"
                              Md="4">
                            <MTextField @bind-Value="_editedItem.Port"   Label="端口号"></MTextField>
                        </MCol>
                        
                    </MRow>
                    <MRow>
                        <MCol Cols="12" Sm="6" Md="12">
                            <MTextarea ClearIcon="mdi-close-circle" required HideDetails="true"
                                       Label="业务说明"
                                       @bind-Value="_editedItem.Remark">

                            </MTextarea>
                        </MCol>
                    </MRow>
                </MContainer>
            </MCardText>

            <MCardActions>
                <MSpacer></MSpacer>
                <MButton Color="blue darken-1"
                         Text
                         OnClick="Close">
                    取消
                </MButton>
                <MButton Color="blue darken-1"
                         Text
                         OnClick="Save">
                    保存
                </MButton>
            </MCardActions>
        </MCard>
    </MDialog>
</MCard>

@code {
    private int _editedIndex = -1;
    private bool show1;
    private bool _loading = true;
    public string FormTitle
    {
        get
        {
            return _editedIndex == -1 ? "新增" : "编辑";
        }
    }
    private SocketConfigDto _editedItem = new SocketConfigDto();
    List<DataTableHeader<SocketConfigDto>> _headers = new()
        {
            new() { Text = "业务编码", Value = nameof(SocketConfigDto.Code), CellClass = "" },
            new() { Text = "Ip地址", Value = nameof(SocketConfigDto.Ip) },
            new() { Text = "端口号", Value = nameof(SocketConfigDto.Port) },
            new() { Text = "说明", Value = nameof(SocketConfigDto.Remark) },
            new() { Text = "状态", Value = nameof(SocketConfigDto.Status) },
            new() { Text = "操作", Value = "actions" }
        };
    bool _visible = false;
    public List<int> _pageSize = new() { 10, 20, 50 };
    public SocketConfigPage userPage = new();
    protected override void OnInitialized()
    {
        Initialize();
    }
    public void Initialize()
    {
        _loading = true;
        //var dto = new Request_Users();
        //if (!string.IsNullOrWhiteSpace(userPage.Search)) dto.Name = userPage.Search;
        //if (_options.ItemsPerPage > 0)
        //{
        //    dto.PageSize = _options.ItemsPerPage;
        //}
        //if (_options.ItemsPerPage < 0)
        //{
        //    dto.PageSize = 1000000;
        //}
        //dto.PageIndex = _options.Page == 0 ? 1 : _options.Page;
        var rest = socketConfigService.GetSocketConfig();
        var result = new SocketConfigPage(rest.Data) { PageIndex = 1, PageSize = 100, PageCount = rest.Data.Count };
        userPage = result;
        _loading = false;
        InvokeAsync(StateHasChanged);
    }
    public void EditItem(SocketConfigDto usersDTO)
    {
        _editedItem = usersDTO;
        _visible = true;
        _editedIndex = 1;
    }
    public async Task DeleteItem(SocketConfigDto usersDTO)
    {
        var result = socketConfigService.DelSocketConfig(usersDTO.Id);
        if (result.IsSuccess)
        {
            await PopupService.ToastAsync("删除成功", AlertTypes.Success);
            userPage.Datas.RemoveAt(userPage.Datas.FindIndex(u => u.Id == usersDTO.Id));
        }
        else
        {
            await PopupService.ToastAsync(result.Msg, AlertTypes.Error);
        }
    }
    public void Close()
    {
        _visible = false;
        _editedItem = new SocketConfigDto();
        _editedIndex = -1;
    }

    public async Task Save()
    {
        ResultEntity<bool> resultEntity = new();
        if (_editedIndex > -1)
        {
            var item = userPage.Datas.Any(x => x.Id == _editedItem.Id);
            if (!item) { await PopupService.ToastAsync("数据不存在", AlertTypes.Warning); return; }
            resultEntity = socketConfigService.UpSocketConfig(new SocketConfigModel() { Id = _editedItem.Id, Code = _editedItem.Code, Ip = _editedItem.Ip, Port = _editedItem.Port,Remark=_editedItem.Remark });
        }
        else
        {
            resultEntity = socketConfigService.AddSocketConfig(new SocketConfigModel() { Code = _editedItem.Code, Ip = _editedItem.Ip, Port = _editedItem.Port,Remark=_editedItem.Remark });

        }
        if (resultEntity.IsSuccess)
        {
            await PopupService.ToastAsync("操作成功", AlertTypes.Success);
            if (_editedIndex == -1) userPage.Datas.Add(_editedItem);
        }
        else
        {
            await PopupService.ToastAsync(resultEntity.Msg, AlertTypes.Error);
        }
        Close();
    }
    
}
